package org.mark.sell.service.impl;

import org.mark.common.response.Result;
import org.mark.sell.entity.RouteInfo;
import org.mark.sell.mapper.RouteInfoMapper;
import org.mark.sell.service.RouteInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RouteInfoServiceImpl implements RouteInfoService {
    @Resource
    private RouteInfoMapper routeInfoMapper;


    @Override
    public Result page(Integer pageNum, Integer pageSize, String siteName) {
        LambdaQueryWrapper<RouteInfo> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(siteName)) {
            wrapper.like(RouteInfo::getSiteName, siteName);
        }
        wrapper.orderByDesc(RouteInfo::getCreateTime);

        Page<RouteInfo> page = routeInfoMapper.selectPage(
                new Page<>(pageNum, pageSize),
                wrapper
        );
        return Result.success(page);
    }
    @Override
    public List<RouteInfo> selectRouteInfoList(RouteInfo routeInfo) {
        QueryWrapper<RouteInfo> routeInfoQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RouteInfo> like = routeInfoQueryWrapper.like("", routeInfo.getSiteName()).like("", routeInfo.getStatus());
        return routeInfoMapper.selectList(like);
    }

    @Override
    public RouteInfo selectRouteInfoById(Integer id) {
        return routeInfoMapper.selectById(id);
    }

    @Override
    public int insertRouteInfo(RouteInfo routeInfo) {
        if(routeInfoMapper.insert(routeInfo) > 0){
            return routeInfo.getId();
        }else {
            return 0;
        }

    }

    @Override
    public int updateRouteInfoById(RouteInfo routeInfo) {
        return routeInfoMapper.updateById(routeInfo);
    }

    @Override
    public int deleteRouteInfoById(Integer id) {
        return routeInfoMapper.deleteById(id);
    }

}
